package com.deductive.web.disposition.layer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.config.RuoYiConfig;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import com.deductive.common.utils.file.FileUploadUtils;
import com.deductive.common.utils.file.FileUtils;
import com.deductive.domain.layer.Models3d;
import com.deductive.framework.config.ServerConfig;
import com.deductive.service.layer.Models3dService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;


@RestController
@RequestMapping("models3d")
@Api(tags = "三维模型管理(舰艇、后送工具关联)")
public class Models3dController extends BaseController {

    @Autowired
    private Models3dService models3dService;

    @Autowired
    private ServerConfig serverConfig;


    /**
     * 分页查询所有数据
     *
     * @param models3d 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("三维模型加载")
    public TableDataInfo list(Models3d models3d) {
        startPage();
        LambdaQueryWrapper<Models3d> eq = new LambdaQueryWrapper<>();
        eq.like(StringUtils.isNotBlank(models3d.getName()), Models3d::getName, models3d.getName())
                .eq(models3d.getId() != null, Models3d::getId, models3d.getId())
                .eq(models3d.getPid() != null, Models3d::getPid, models3d.getPid())
                .eq(StringUtils.isNotBlank(models3d.getType()), Models3d::getType, models3d.getType());
        List<Models3d> list = models3dService.list(eq);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("三维模型单个查询")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(this.models3dService.getById(Integer.parseInt(id.toString())));
    }

    /**
     * 新增数据
     *
     * @param models3d 实体对象
     * @return 新增结果
     */
    @ApiOperation("三维模型添加")
    @PostMapping
    public AjaxResult insert(@RequestBody Models3d models3d) {
        return AjaxResult.success(this.models3dService.save(models3d));
    }

    @ApiOperation("三维模型上传")
    @PostMapping("/modelImport")
    public AjaxResult modelImport(@RequestPart("file") MultipartFile file) {

        try {
            // 上传文件路径
            String filePath = RuoYiConfig.get3DModel();
            System.out.println(filePath);
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            System.out.println(fileName);
            System.out.println(file.getName());
            String url = serverConfig.getUrl() + fileName;
            System.out.println(url);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());

            Models3d models3d = new Models3d();
            models3d.setUrl(url);
            models3d.setName(file.getOriginalFilename());
            models3dService.save(models3d);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 修改数据
     *
     * @param models3d 实体对象
     * @return 修改结果
     */
    @ApiOperation("三维模型修改")
    @PutMapping
    public AjaxResult update(@RequestBody Models3d models3d) {
        return AjaxResult.success(this.models3dService.updateById(models3d));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("三维模型删除")
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return AjaxResult.success(this.models3dService.removeByIds(idList));
    }

}
